/*
 * @Author: wuqingchun
 * @Date: 2024-10-29 20:15:18
 * @Description: 
 * @LastEditors: wuqingchun
 * @LastEditTime: 2024-10-31 20:35:59
 */
#ifndef __NET_CONNECTION_BASE_H__
#define __NET_CONNECTION_BASE_H__

#include "libnet.h"
#include "net_buffer.h"
#include "net_event_handler.h"

class CNetConnectionBase : public INetTCPConnection
{
    // 连接状态定义
    enum EnumConnState
    {
        CONN_STATE_UINIT,
        CONN_STATE_INIT,
        CONN_STATE_CONNECTING,
        CONN_STATE_CONNECTED,
        CONN_STATE_DISCONNECTED,
    };

    enum EnumConnType
    {
        CONN_ACTIVE,
        CONN_PASSIVE,
    };
public:

    CNetConnectionBase(INetEventHandler& refEventHandler, INetTCPConnectionCallback& refCallback, const sockaddr_in& refAddr);

public:
    virtual int GetFd() override final;
    virtual int32_t DealRDEvent() override;
    virtual int32_t DealWREvent() override;
    virtual int32_t DealERREvent() override;
    virtual int32_t Send(const void* pData, size_t ulDataLen) override final;
    virtual void Close() override final;

protected:
    int32_t InitActive();
    int32_t InitPassive(int iListenerFd);
private:
    int32_t __Connect();
    ssize_t __RecvOne(void* pBuf, size_t ulBufLen);
    ssize_t __SendOne(const void* pData, size_t ulDataLen);
    void __SendBuffer();
private:
    INetEventHandler& m_refEventHandler;
    INetTCPConnectionCallback& m_refCallback;
    sockaddr_in m_stAddr;
    int m_iSockFd = -1;
    EnumConnType m_enType = EnumConnType::CONN_ACTIVE;
    EnumConnState m_enState = EnumConnState::CONN_STATE_UINIT;
    CNetBuffer m_objSendBuffer;   // 发送缓冲区
    
};

#endif //__NET_CONNECTION_BASE_H__